package com.jpcc.vw.onlineRadio.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.media.session.MediaSessionCompat;
import android.view.KeyEvent;

import com.jpcc.vw.onlineRadio.service.AlbumRadioAudioPlayer;
import com.jpcc.vw.onlineRadio.utils.PreferenceUtils;
import com.jpcc.vw.onlineRadio.utils.ToastUtils;


/**
 * 耳机线控，仅在5.0以下有效，5.0以上被{@link MediaSessionCompat}接管。
 * Created by hzwangchenyan on 2016/1/21.
 */
public class RemoteControlReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null || event.getAction() != KeyEvent.ACTION_UP) {
            return;
        }
        if (!PreferenceUtils.GetAudioFocusStatus()) {
            return;
        }

        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_MEDIA_PLAY:
//                ToastUtils.show("RemoteControlReceiver-->"+"播放");
//                AlbumRadioAudioPlayer.get().playPause();
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
//                ToastUtils.show("RemoteControlReceiver-->"+"暂停");
//                AlbumRadioAudioPlayer.get().playPause();
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                ToastUtils.show("RemoteControlReceiver-->" + "暂停");
                AlbumRadioAudioPlayer.get().playPause();
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
//                AudioPlayer.get().playPause();
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                ToastUtils.show("RemoteControlReceiver-->" + "下一首");
                AlbumRadioAudioPlayer.get().next();
//                AudioPlayer.get().next();
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                ToastUtils.show("RemoteControlReceiver-->" + "上一首");
                AlbumRadioAudioPlayer.get().prev();
//                AudioPlayer.get().prev();
                break;
            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
                ToastUtils.show("RemoteControlReceiver-->" + "快进");
//                AlbumRadioAudioPlayer.get().fastForward();
                break;
            case KeyEvent.KEYCODE_MEDIA_REWIND:
                ToastUtils.show("RemoteControlReceiver-->" + "快退");
//                AlbumRadioAudioPlayer.get().fastBack();
                break;

        }
    }
}
